/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.presentationappearance;

import org.openaecfm.model.geometry.CartesianPoint;
import org.openaecfm.model.geometry.Vector;
import org.openaecfm.model.measure.PositiveLengthMeasure;
import org.openaecfm.model.presentationappearance.CurveStyle;
import org.openaecfm.model.presentationappearance.FillAreaStyleHatching;
import org.openaecfm.spf.io.AbstractEntityInstanceInputImpl;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceOutput;
import org.openaecfm.spf.model.DollarParameter;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.EntityInstanceNameParameter;
import org.openaecfm.spf.model.NodeType;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.RealParameter;
import org.openaecfm.spf.model.SimpleEntityInstance;
import org.openaecfm.spf.model.TypedParameter;

public final class FillAreaStyleHatchingIO extends
		AbstractEntityInstanceInputImpl<FillAreaStyleHatching> implements
		IEntityInstanceOutput<FillAreaStyleHatching> {
	/**
	 * {@inheritDoc}
	 */
	@Override
	public String exportSPF(FillAreaStyleHatching pojo,
			IAppDataManager appDataManager) {
		SimpleEntityInstance ei = new SimpleEntityInstance(
				"IFCFILLAREASTYLETILES", appDataManager.getName(pojo));

		// Set HatchLineAppearance
		ei.addParameter(new EntityInstanceNameParameter(appDataManager
				.getName(pojo.getHatchLineAppearance())));

		// Set StartOfNextHatchLine
		if (pojo.getStartOfNextHatchLine() instanceof Vector) {
			ei.addParameter(new EntityInstanceNameParameter(appDataManager
					.getName(pojo.getStartOfNextHatchLine())));
		}
		if (pojo.getStartOfNextHatchLine() instanceof PositiveLengthMeasure) {
			TypedParameter t = new TypedParameter();
			t.setKeyword("IFCPOSITIVELENGTHMEASURE");
			t.setParameter(new RealParameter(((PositiveLengthMeasure) pojo
					.getStartOfNextHatchLine()).doubleValue()));
			ei.addParameter(t);
		}

		// Set PointOfReferenceHatchLine
		if (pojo.getPointOfReferenceHatchLine() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new EntityInstanceNameParameter(appDataManager
					.getName(pojo.getPointOfReferenceHatchLine())));
		}

		// Set PatternStart
		if (pojo.getPatternStart() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new EntityInstanceNameParameter(appDataManager
					.getName(pojo.getPatternStart())));
		}

		// Set HatchLineAngle
		ei.addParameter(new RealParameter(pojo.getHatchLineAngle()));

		return ei.toString();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected FillAreaStyleHatching doCreateObject() {
		return new FillAreaStyleHatching();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void resolveReferenceParameter(EntityInstance ei,
			FillAreaStyleHatching pojo, IAppDataManager appDataManager) {
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set HatchLineAppearance
		EntityInstanceNameParameter happ = (EntityInstanceNameParameter) sei
				.getParameter(0);
		pojo.setHatchLineAppearance((CurveStyle) appDataManager.getObject(happ
				.toInstanceName()));

		// Set StartOfNextHatchLine
		Parameter starthl = sei.getParameter(1);
		if (starthl.getNodeType() == NodeType.ENTITY_INSTANCE_NAME) {
			EntityInstanceNameParameter n = (EntityInstanceNameParameter) starthl;
			pojo.setStartOfNextHatchLine(appDataManager.getObject(n
					.toInstanceName()));
		}
		if (starthl.getNodeType() == NodeType.TYPED_PARAMETER) {
			TypedParameter t = (TypedParameter) starthl;
			RealParameter r = (RealParameter) t.getParameter();
			pojo.setStartOfNextHatchLine(new PositiveLengthMeasure(r.toDouble()));
		}

		// Set PointOfReferenceHatchLine
		Parameter pointhl = sei.getParameter(2);
		if (pointhl.getNodeType() == NodeType.ENTITY_INSTANCE_NAME) {
			EntityInstanceNameParameter n = (EntityInstanceNameParameter) pointhl;
			pojo.setPointOfReferenceHatchLine((CartesianPoint) appDataManager
					.getObject(n.toInstanceName()));
		}

		// Set PatternStart
		Parameter patternstart = sei.getParameter(3);
		if (patternstart.getNodeType() == NodeType.ENTITY_INSTANCE_NAME) {
			EntityInstanceNameParameter n = (EntityInstanceNameParameter) patternstart;
			pojo.setPatternStart((CartesianPoint) appDataManager.getObject(n
					.toInstanceName()));
		}
	}

	@Override
	protected void resolveNotReferenceParameter(EntityInstance ei,
			FillAreaStyleHatching pojo) {
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set HatchLineAngle
		RealParameter hatchangle = (RealParameter) sei.getParameter(4);
		pojo.setHatchLineAngle(hatchangle.toDouble());
	}
}
